package ru.kipriz.todoly.api.dao.http;

import ru.kipriz.todoly.api.AccessDeniedException;
import ru.kipriz.todoly.api.Parser;
import ru.kipriz.todoly.api.dao.ProjectObjectDao;
import ru.kipriz.todoly.api.entities.ItemObject;
import ru.kipriz.todoly.api.entities.ProjectObject;

import java.io.IOException;
import java.util.List;

/**
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * @author Andrey Volkov
 *         <br/>Date: 12/13/10
 */
public class ProjectDaoHttp implements ProjectObjectDao
{
    public List<ProjectObject> getAll()
    {
        try
        {
            String response = WebRequest.get("projects.json");
            return Parser.parseListJson(ProjectObject.class, response);
        } catch (IOException e)
        {
            return null;
        }
    }

    public ProjectObject createProject(ProjectObject project, String... fields)
    {
        try
        {
            String response = WebRequest.post("projects.json", Parser.toJson(project, fields));
            return Parser.parseJson(ProjectObject.class, response);
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public ProjectObject getById(long id) {
        try {
            String response = WebRequest.get("projects/" + id + ".json");
            return Parser.parseJson(ProjectObject.class, response);
        } catch (AccessDeniedException ade) {
            throw ade;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public ProjectObject delete(long id)
    {
        try
        {
            String response = WebRequest.delete("projects/" + id + ".json");
            return Parser.parseJson(ProjectObject.class, response);
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public ProjectObject update(ProjectObject project, String... fields)
    {
        try
        {
            String response = WebRequest.post("projects/" + project.getId() + ".json", Parser.toJson(project, fields));
            return Parser.parseJson(ProjectObject.class, response);
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }

    }

    public List<ItemObject> getItems(long id)
    {
        try
        {
            String response = WebRequest.get("projects/" + id + "/items.json");
            return Parser.parseListJson(ItemObject.class, response);
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public List<ItemObject> getDoneItems(long id)
    {
        try
        {
            String response = WebRequest.get("projects/" + id + "/doneitems.json");
            return Parser.parseListJson(ItemObject.class, response);
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }

    }
}
